package slap.mvc.router;

import java.util.List;
import java.util.Vector;

import jregex.Matcher;
import jregex.Pattern;

import slap.http.Request;
import slap.mvc.Action;
import slap.util.Asserts;

public class RegexRouter implements Router {
	
	private List<Action> routes = new Vector<Action>();
	
	@Override
	public void add(Action action) {
		Asserts.notNull(action.regex());
		routes.add(action);
	}

	@Override
	public Action route(Request request) {
		String path = request.path();
		for(Action action : routes){
			Matcher matcher = action.regex().matcher(path);
			if(matcher.matches()){
				return action;
			}
		}
		return null;
	}

	@Override
	public boolean contains(Action action) {
		for(Action a : routes){
			if(a.regex().toString().equals(action.regex().toString())){
				return true;
			}
		}
		return false;
	}

}
